#define _CRT_SECURE_NO_WARNINGS 1

#include<string>
#include<vector>
#include<queue>
#include<unordered_map>
using namespace std;

int bfs(string begin, string end, vector<string>& v);

int ladderLength(string beginWord, string endWord, vector<string>& wordList)
{
    int ret = bfs(beginWord, endWord, wordList);
    return ret;
}


int bfs(string begin, string end, vector<string>& v)
{
    unordered_map<string, int> hash1;
    unordered_map<string, int> hash2;
    char start = 'a';
    int count = 0;
    queue<string> q;
    hash1[begin]++;
    for (auto& e : v)
    {
        hash2[e]++;
    }
    q.push(begin);
    while (q.size())
    {
        int sz = q.size();
        count++;
        for (int i = 0; i < sz; i++)
        {
            string s = q.front();
            q.pop();
            for (int j = 0; j < begin.size(); j++)
            {
                string temp = s;
                for (int k = 0; k < 26; k++)
                {
                    temp[j] = start + k;
                    if (hash1[temp] == 0 && hash2[temp] > 0)
                    {
                        if (temp == end)
                        {
                            return count + 1;
                        }

                        q.push(temp);
                        hash1[temp]++;
                    }
                }
            }
        }
    }
    return 0;
}